<template>
	<view class="login-container">
		<uni-icons type="contact-filled" size="100" color="#AFAFAF"></uni-icons>
		<button type="primary" class="btn-login" open-type="getUserInfo" @click="getUserProfile">一键登录</button>
		<text class="tips-text">登录后尽享更多权益</text>
	</view>
</template>

<script>
	import {
		mapMutations,
		mapState
	} from 'vuex'
	export default {
		name: "my-login",
		data() {
			return {

			};
		},
		computed: {
			...mapState('my_user', ['token', 'redirectinfo'])

		},
		methods: {
			...mapMutations('my_user', ['updateUserInfo']),
			...mapMutations('my_user', ['updateToken', 'updateRedirectinfo']),
			getUserProfile() {
				uni.getUserProfile({
					desc: '请求授权',
					success: (res) => {
						console.log(res)
						this.updateUserInfo(res.userInfo)
						this.getToken()
						this.navigateBack()
					},
					fail: () => {
						return uni.$showMsg('您取消了登录授权')
					}
				})
			},
			getToken() {
				// 解构出成功和失败的对象
				// const res = await uni.login().catch(err => {
				// 	if (err || err.errMsg !== 'login: ok') return uni.$showMsg('登录失败！')
				// })

				// 准备参数
				// const query = {
				// 	code: res.code,
				// 	encryptedData: info.encryptedData,
				// 	iv: info.iv,
				// 	rawData: info.rawData,
				// 	signature: info.signature
				// }
				// console.log(res.code)

				//换取token
				this.updateToken(
					'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo'
				)

			},
			navigateBack() {
				console.log(this.redirectinfo)

				uni.switchTab({
					url: this.redirectinfo.from,
					complete: () => {
						this.updateRedirectinfo(null)
					}
				})
			}




		}
	}
</script>

<style lang="scss">
	.login-container {
		height: 750rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		background-color: #f8f8f8;
		position: relative;
		overflow: hidden;

		.btn-login {
			margin-top: 20px;
			width: 80%;
			height: 40px;
			line-height: 40px;
			border-radius: 20px;
			background-color: #ef3607;
			color: white;
			font-size: 20px;
		}

		.tips-text {
			margin-top: 10px;
			font-size: 12px;
			color: gray;
		}
	}
</style>